package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.MaterialDetail;
import com.synergy.stockflow.product.entity.ProcessDetail;
import com.synergy.stockflow.product.entity.ProcessName;
import com.synergy.stockflow.product.service.MaterialDetailService;
import com.synergy.stockflow.product.service.ProcessDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/materialDetail")
public class MaterialDetailController {
    @Autowired
    private MaterialDetailService materialDetailService;

    @GetMapping("/index")
    public Result<Page<MaterialDetail>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                             @RequestParam(name = "size", defaultValue = "5") int size,
                                             String processRouteId
    ){
        {
            QueryWrapper<MaterialDetail> query =new QueryWrapper<>();
            query.lambda().eq(MaterialDetail::getProcessRouteId,processRouteId);
//            query.lambda().eq(MaterialDetail::getDetailId,detaialId);
            Page<MaterialDetail> page = new Page<>(current,size);
            page = materialDetailService.page(page,query);
            return Result.success(page);
        }
    }
//    @GetMapping("/Name")
//    public Result<Page<ProcessName>>getName(String detailId){
//        System.out.println(detailId);
//        Page<ProcessName> page = new Page<>(1, 10000);
//        page=processDetailService.Name(page,detailId);
//        return Result.success(page);
//    }
    @PostMapping("insert")
    public Result insert(@RequestBody MaterialDetail materialDetail){
        System.out.println(materialDetail);
        materialDetailService.save(materialDetail);
        return Result.success(materialDetail);
    }
    @PutMapping("update")
    public Result update(@RequestBody MaterialDetail materialDetail){
        System.out.println(materialDetail);
        materialDetailService.updateById(materialDetail);
        return Result.success(materialDetail);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<MaterialDetail> materialDetail){

        for (MaterialDetail detail:materialDetail){
        }

        for(MaterialDetail detail:materialDetail){
            materialDetailService.removeById(detail.getDetailId());
        }
        return Result.success(materialDetail);
    }
}